﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sungero.Core;
using Sungero.CoreEntities;
using akibank.Dossier.DossierContract;

namespace akibank.Dossier.Shared
{
  partial class DossierContractFunctions
  {

    /// <summary>
    /// 
    /// </summary>       
    public void SetStatePropertiesLoanApplication()
    {
      if (Functions.DossierContract.Remote.DokumentKindCheck(_obj, Constants.Module.PledgeContractKind))
      {
        _obj.State.Properties.LoanContract.IsRequired = true;
        _obj.State.Properties.LoanContract.IsVisible = true;
      }
      else 
      {
        if (_obj.LoanContract != null)
        {
          _obj.LoanContract = null;
        }
        _obj.State.Properties.LoanContract.IsRequired = false;
        _obj.State.Properties.LoanContract.IsVisible = false;
      }
      
      if (Functions.DossierContract.Remote.DokumentKindCheck(_obj, Constants.Module.LoanApplicationKind))
      {
        if (!string.IsNullOrWhiteSpace(_obj.DocNum))
        {
          _obj.DocNum = string.Empty; 
        }
        _obj.State.Properties.DocNum.IsRequired = false;
        _obj.State.Properties.DocNum.IsVisible = false;
        _obj.State.Properties.Subject.IsRequired = true;
      }
      else 
      {
        _obj.State.Properties.DocNum.IsRequired = true;
        _obj.State.Properties.DocNum.IsVisible = true;
        _obj.State.Properties.Subject.IsRequired = false;
      }
    }

    /// <summary>
    /// 
    /// </summary>       
    public override void FillName()
    {
      var isApplicant = Functions.DossierContract.Remote.DokumentKindCheck(_obj, Constants.Module.LoanApplicationKind);
      //остановился тут, проверить как будет именовать при пустых полях
      //TODO: от вида документа формируется имя
      if (!string.IsNullOrWhiteSpace(_obj.DocNum) && _obj.Branch != null)
        _obj.Name = string.Format("{0}-{1}", _obj.Branch.Code, _obj.DocNum);
      if (isApplicant = true && !string.IsNullOrWhiteSpace(_obj.Subject) && _obj.Counterparty != null)
          _obj.Name = string.Format("КЗ-{0}-{1}", _obj.Counterparty.Name, _obj.Subject);
    }

  }
}